package com.example.module.module.category.service;

import com.example.module.module.category.entity.Category;
import com.example.module.module.category.mapper.CategoryMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    public List<Category> getRootCategories() {
        return categoryMapper.findByParentId(null);
    }

    public List<Category> getChildren(Long parentId) {
        return categoryMapper.findByParentId(parentId);
    }

    public List<BigInteger> getCategoryIdByKeyword(String keyword) {

        if (keyword == null || keyword.trim().isEmpty()) {
            return Collections.emptyList();
        }
        return categoryMapper.findCategoryIdByKeyword(keyword);
    }

    public List<Category> listByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
        return categoryMapper.selectByIds(ids);
    }
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    public Category getById(Long categoryId) {
        return categoryMapper.getById(categoryId);
    }

    public List<Category> extractById(Long categoryId) {
        return categoryMapper.extractById(categoryId);
    }

    /*public Integer insert(Category entity) {
        return categoryMapper.insert(entity);
    }

    public Integer update(Category entity) {
    return categoryMapper.update(entity);
    }

     */

    //合并新增和修改为一个
    public Long edit(Long categoryId, String name, String image) {

        validateParameters(categoryId, name, image);

        int timestamp = (int) (System.currentTimeMillis() / 1000);
        Category category = new Category();
        category.setName(name);
        category.setImage(image);

        if (categoryId != null) {
            category.setId(categoryId);
            Category category1 = categoryMapper.getById(categoryId);
            if (category1 == null) {
                throw new RuntimeException("categoryId对应的记录不存在");
            }

            category.setUpdateTime(timestamp);
            categoryMapper.update(category);
            return categoryId;
        }else {
            category.setUpdateTime(timestamp);
            category.setCreateTime(timestamp);
            category.setIsDeleted(0);
            categoryMapper.insert(category);
            return category.getId();
        }
    }

    //验证参数
    private void validateParameters(Long categoryId, String name, String image) {
        if (name == null || name.trim().isEmpty()) {
            throw new RuntimeException("类目名不能为空");
        }
        if (image == null || image.trim().isEmpty()) {
            throw new RuntimeException("图片不能为空");
        }
    }
    public Integer countChildren(Long categoryId) {
        return categoryMapper.countChildren(categoryId);
    }
    public Integer delete(Long categoryId) {

        if (categoryMapper.countChildren(categoryId)>0){
            throw new RuntimeException("请先删除子分类");
        }
        return categoryMapper.delete((int)(System.currentTimeMillis() / 1000),categoryId);
    }


}

